package cn.edu.lsnu.mbti.controller;

import cn.edu.lsnu.mbti.common.R;
import cn.edu.lsnu.mbti.dto.DepartmentDto;
import cn.edu.lsnu.mbti.dto.UserDto;
import cn.edu.lsnu.mbti.entity.Department;
import cn.edu.lsnu.mbti.entity.vo.DepartmentVo;
import cn.edu.lsnu.mbti.exception.CustomerException;
import cn.edu.lsnu.mbti.service.DepartmentCategoryService;
import cn.edu.lsnu.mbti.service.DepartmentService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/department")
@Slf4j
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;
    @Resource
    private DepartmentCategoryService departmentCategoryService;

    /**
     * vue后台管理删除学科
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<String> delete(@PathVariable("id") String id){
        try {
            departmentService.removeById(id);
        } catch (Exception e) {
            throw new CustomerException("删除失败，请稍后重试");
        }
        return R.success("删除成功");
    }


    /**
     * vue后台管理保存学科
     * @param department
     * @return
     */
    @PostMapping("saveDepartment")
    public R<String> saveDepartment(@RequestBody Department department){
        log.info("department:{}",department);
        if (BeanUtil.isEmpty(department)){
            throw new CustomerException("无法保存空对象");
        }
        return R.success(departmentService.saveDepartment(department));
    }


    /**
     * vue后台管理分页查询学科列表
     * @param departmentVo
     * @return
     */
    @PostMapping("queryPageList")
    public R<Page<DepartmentDto>> queryPageList(@RequestBody DepartmentVo departmentVo){
        return R.success(departmentService.queryPageList(departmentVo));
    }


    /**
     * 根据分类id查询不同的学科集合接口
     * @param categoryId
     * @return
     */
    @GetMapping("queryDepartmentListByCategoryId")
    public R<List<DepartmentDto>> queryDepartmentListByCategoryId(@RequestParam String categoryId){
        return R.success(departmentCategoryService.queryDepartmentListByCategoryId(categoryId));
    }


    /**
     * 展示推荐的学科接口
     * @param userDto
     * @return
     */
    @PostMapping("selectDepartmentByRecommend")
    public R<List<DepartmentDto>> selectDepartmentByRecommend(@RequestBody UserDto userDto){
        return R.success(departmentService.selectDepartmentByRecommend(userDto.getId()));
    }


    @GetMapping("/list")
    public R<List<Department>> list(){
        return R.success(departmentService.queryList());
    }
}
